<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>AES、RSA加密</title>
    <script th:src="@{/js/jquery-3.6.0.js}"></script>
    <script th:src="@{/js/crypto-js.js}"></script>
    <script th:src="@{/js/jsencrypt.js}"></script>
</head>
<body>
<h1>加密算法demo</h1>
<h2>AES加密，后台解密</h2>
<input id="aesValue"/>
<button onclick="encryptAes()">加密AES，返回解密后的明文</button>

<h2>RSA加密，后台解密</h2>
<input id="rsaValue"/>
<button onclick="encryptRsa()">加密RSA，返回解密后的明文</button>
</body>
<script>
    /**
     * AES 加密方法
     */
    function encryptAes() {
        // 密钥，后端也用相同的密钥解密
        var secretKey = 'uBdUx82vPHkDKb284d7NkjFoNcKWBuka';
        var encryptedData = CryptoJS.AES.encrypt($("#aesValue").val(), CryptoJS.enc.Utf8.parse(secretKey), {
            iv: CryptoJS.enc.Utf8.parse('1234567887654321'),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        // 加密内容要toString()，返回的才是加密的字符串
        console.info(encryptedData.toString());

        // 后台解密
        $.ajax({
            type: "get",
            url: "/demo/decryptAes",
            data: {value: encryptedData.toString()},
            success: function(data) {
                alert('解密内容是：' + data);
            },
            error: function() {
            }
        });
    }

    /**
     * RSA 加密方法
     */
    function encryptRsa() {
        var encrypt = new JSEncrypt();
        // 设置公钥
        encrypt.setPublicKey('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsRVQfE0mOFCBqthPyNLzJTtENgRAw2O4g7RhWQhms3P+uKHt+Kuff+YaHKpAQ6XiyPmJSAS9joaemvF5ew9k3ZGuDYnXNd3Vwxq9bTHnPAZeTLzVQ1n5iOmid1BR56ENynLkw8LSHgcHPixwvXfJNVHczip/1aXqwAmZS1GKLnJXye1+qv+WeWMcLOAYRVi1sm+nhRV+TzN3o9ykPqiEY8bmAOWo//jD68YgVRWwlnOY+euGzfWV4mhxyVSJ1jjwvM5Aox+HfABHnWPQ/3DdTmtM7NJKAYVhCZOOgQAY/os2OBf8AIC6uHmwFkJvUS4YkUPS5kRE+s1SR3iIxaAeEwIDAQAB');
        // 使用公钥加密，返回加密后的数据
        var encrypted = encrypt.encrypt($("#rsaValue").val());
        console.info(encrypted);

        // 前台解密
        var decrypt = new JSEncrypt();
        // 设置私钥
        decrypt.setPrivateKey('MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCxFVB8TSY4UIGq2E/I0vMlO0Q2BEDDY7iDtGFZCGazc/64oe34q59/5hocqkBDpeLI+YlIBL2Ohp6a8Xl7D2Tdka4Nidc13dXDGr1tMec8Bl5MvNVDWfmI6aJ3UFHnoQ3KcuTDwtIeBwc+LHC9d8k1UdzOKn/VperACZlLUYouclfJ7X6q/5Z5Yxws4BhFWLWyb6eFFX5PM3ej3KQ+qIRjxuYA5aj/+MPrxiBVFbCWc5j564bN9ZXiaHHJVInWOPC8zkCjH4d8AEedY9D/cN1Oa0zs0koBhWEJk46BABj+izY4F/wAgLq4ebAWQm9RLhiRQ9LmRET6zVJHeIjFoB4TAgMBAAECggEAXzpSFPY8Bm6svF4k0Z83aB8t+EpjNW+r6w+WhhVxfTF0mn9iBKbuWA46no2jj31mrqMagyMheMLGChavOFE38bsEuX0VwX7xFEGN+mmqqunVzjiCCv0YptrVJoqH8SL30pXkla1v5vvqljG50vnWtxi1wBy0sATQb8L5r7pCr+SiNAlbt2X7ePyk7EkKTpon6Bl2249dZnDedOdz+wH/lQs9rGxvtqeBqbqWCj7GsxEuQysSBtnvsta/vw7M+/VvEiRh5MUrCVvJrt/thJwWMY57e01iHLuURf6xnrZqvYPrCcz5i2MVbXVawyCIq5TJAJLyeGRkXGXIvtqbkM3xAQKBgQDfYq0i4yWmnB88NehdnnTb3Tmtxtd3A+2tHzJF3dYAc1a6zAFXqTDPbt5SsqoonIbbBAzdvREHCWw4yc8XGIx18/tdxeF385V/umpPkhM+v+QYmFOLDjdODD3JByIccMMW392Zrhe6A1JA5b83XjUJs1wwFogrIY/HWElHlZ1tkwKBgQDK8Ahvl0mRnbSgA6aQ1iUzOiz7tEZRHFiep0kis8pLsePxL3UsKXP5rgssMk+hj4oLsDqRi9OXyKVFIy7N/b7DNUqnAG9y8w1ASZYvHdgJ3GndjOSESWQia83RDxaVTHP+6AQVkcSoz6HjA/JZh3FX34ogyThX/g10eEeuD/zdgQKBgDAGvQaFI3Egm66MQHrwOFuUwnPUDjruQ0k3Lc3Lldje80SR27AHFhFi2rWY5/B3Rp8LzeBWZ7TFl9jVuopSotAxNlfA1Wq+meOuJnEZDL4u71FCXqPE2KsVAquXtwO6Upc2ZiRGQ5+gWyzMuEGlJW4Ce81y6q+rNYg8OPUE+6W1AoGAXuSwvSV8BDg/PteWKoHIMVURtb0NaF4d3koQ+8YgYRbUpHHxk23qyiSlGsyHyd8pyN9l86rzjTUIevTBrRbq/bLMrpyYgKLAXoBKVsvYunuzkSKRtH3w8duBCTHJ+JQgZWCukDtyPxGf/FxbFZws1s8zcrNEwniMaBraQb0+G4ECgYANbFMZEFxZ/zy3BAiWQvGMWTKTocopmaMGhvHrqoSvsXea4HFBPfq1YF32SzamB+5xVb6ePWLK7Vl8wYSHXLkyh6kWO8FJz9Q3VmmfGhMQe17+x4/D6zPg68cKmghhnzupKRFv2pZPpNIyGZ2GRpG28+AtIvnjWFzi1NJSowKe6w==');
        var uncrypted = decrypt.decrypt(encrypted);
        console.info(uncrypted);

        // 后台解密
        $.ajax({
            type: "get",
            url: "/demo/decryptRsa",
            data: {value: encrypted},
            success: function(data) {
                alert('解密内容是：' + data);
            },
            error: function() {
            }
        });
    }
</script>
</html>